package com.yaosheng.thread.thread;

import java.lang.ref.SoftReference;

/**
 * Created by yaosheng on 2021/7/2.
 * 软引用:当内存中数据过大时，gc会回收该部分数据
 */
public class TestSoftReference {

    public static void main(String[] args) {

        SoftReference<byte[]> m = new SoftReference<> (new byte[1024 * 1024 * 10]);
        System.out.println (m.get ());
        System.gc ();
        try{
            Thread.sleep (500);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        System.out.println (m.get ());

        //再分配一个数组，heap将装不下，这时候系统将会进行垃圾回收，先回收一次，如果不够，再把软引用干掉
        byte[] b = new byte[1024 * 1024 * 15];
        System.out.println (m.get ());
    }
}
